<template>
  <BaseMainSlot>
    <template v-slot:btnsBar></template>
    <template v-slot:treebox>
      <typeTree
        :page-no="139"
        @queryDataBytype="queryDataBytype"
        @updataTable="queryPageList()"
        @updataTable1="updataTable1()"
      />
    </template>
    <template v-slot:searchbox>
      <div class="searchbox">
        <div class="select-box-content">
          <div class="select-box-content-name">型号名称：</div>
          <el-input
            v-model="modelName"
            clearable
            placeholder="请输入型号名称"
            @change="queryPageList()"
          />
        </div>
        <ActionBar
          :add-active="addActive"
          :del-active="delActive"
          :has-edit="false"
          :has-cancel="false"
          :has-save="false"
          :page-no="138"
          @add="add"
          @del="del"
        />
      </div>
    </template>
    <template v-slot:tablebox>
      <el-table
        ref="tableData"
        v-loading="loading"
        highlight-current-row
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        :row-key="
          (row) => {
            return row.equModelCode;
          }
        "
        @row-click="handleEdit"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" :reserve-selection="true" />
        <el-table-column type="index" label="序号" width="50">
          <template
            slot-scope="scoped"
          >{{ scoped.row.equModelCode ? scoped.$index + 1 : "" }}</template>
        </el-table-column>
        <el-table-column prop="modelName" label="型号名称" />
        <el-table-column prop="equipmentTypeName" label="所属类型" />

        <el-table-column label="操作" width="300">
          <template slot-scope="scoped">
            <div class="table-btns-box">
              <div
                v-if="scoped.row.equModelCode"
                class="table-btns edit-btns"
                @click="edit(scoped.row)"
              >修改</div>
              <div
                v-if="
                  scoped.row.equModelCode && judgePermission(139, 'deleteone')
                "
                class="table-btns del-btns"
                @click="del(scoped.row)"
              >删除</div>
              <div
                v-if="scoped.row.equModelCode"
                class="table-btns detail-btns"
                @click="queryDetail(scoped.row)"
              >点击查看</div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
      <AddEditDialog
        ref="addEditDialog"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancelDialog"
      />
      <!-- <TreeAddEditDialog
        ref="TreeAddEditDialog"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :parent-code="parentCode"
        :edit-object="treeEditObject"
        :show.sync="showTreeAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancelDialog"
      />-->
      <ShowDetailDialog
        ref="ShowDetailDialog"
        :equ-model-template-list="equModelTemplateList"
        :show.sync="showDetailDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancelDialog"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import BaseMainSlot from "@/components/BaseMainSlot/tree-type.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import ShowDetailDialog from "./components/show-detail-dialog.vue";
import { mymixin } from "@/utils/mixin";
// import equipTypeApi from "@/api/processing-management/equip-manage/equip-type/index";
import equipTemplateApi from "@/api/processing-management/equip-manage/equip-template";
import equipModelApi from "@/api/processing-management/equip-manage/equip-model/index";
import typeTree from "./components/type-tree.vue";
import { mapGetters } from "vuex";

export default {
  name: "EquipTemplate",
  components: {
    ActionBar,
    Pagination,
    BaseMainSlot,
    AddEditDialog,
    ShowDetailDialog,
    typeTree
  },
  mixins: [mymixin],
  data() {
    return {
      // 控制弹出框显示隐藏
      showAddEditDialog: false,
      showDetailDialog: false,
      // 搜索条件:型号名称：
      modelName: "",
      checkedIdList: [],
      equModelTemplateList: []
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    this.queryPageList();
  },
  mounted() {},
  methods: {
    updataTable1() {
      this.equipmentTypeCode = null;
      this.currentPage = 1;
      this.queryPageList();
    },
    // 查看详情
    queryDetail(row) {
      let data = JSON.parse(JSON.stringify(row));
      this.equModelTemplateList = data.equModelTemplateList || [];
      this.showDetailDialog = true;
    },
    queryDataBytype(data) {
      this.equipmentTypeCode = data;
      this.queryPageList();
    },
    updataTable() {
      this.queryPageList();
    },
    queryPageList() {
      this.loading = false;
      // 获取数据
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          modelName: this.modelName,
          equipmentTypeCode: this.equipmentTypeCode
        }
      };
      equipTemplateApi.queryPageList(param).then(res => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data;
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.queryPageList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    handleEdit(row) {
      if (row.equModelCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.addActive = true;
        this.editActive = true;
      } else {
        this.editObject = {};
        this.addActive = true;
        this.editActive = false;
      }
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      this.checkedIdList = list.map(item => item.equModelCode);
      if (this.checkedIdList.length > 0) this.delActive = true;
      else this.resetActionBar();
    },
    // 表格删除
    delmessage() {
      var form = new FormData();
      this.checkedIdList.forEach(a => {
        form.append("equModelCodeList", a);
      });
      equipModelApi.delList(form).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.editActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.equModelCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.editActive = false;
        this.showAddEditDialog = true;
      }
    },
    del(data) {
      this.$confirm("此操作将永久删除该类型, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          var form = new FormData();
          form.append("equModelCodeList", data.equModelCode);
          equipModelApi.delList(form).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.queryPageList();
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    closeDialog() {
      if (this.showTreeAddEditDialog === true) {
        this.getTreeDataList();
      }
      if (this.showAddEditDialog === true) {
        this.queryPageList();
      }
      this.cancelDialog();
    },
    cancelDialog() {
      if (this.showAddEditDialog === true) {
        this.queryPageList();
      }
      this.showAddEditDialog = false;
      this.showTreeAddEditDialog = false;
      this.showDetailDialog = false;
      this.nowChanceStau = "";
      this.titleText = "";
      this.editObject = {};
      this.addActive = true;
      this.delActive = false;
      this.editActive = false;
      this.$refs.tableData.setCurrentRow();
      this.$refs.tableData.clearSelection();
    }
  }
};
</script>

<style lang="scss" scoped>
.searchbox {
  width: 100%;
  background-color: #00485a;
  display: flex;
}
::v-deep .el-table {
  .el-icon {
    color: #fff;
  }
  .el-table__expanded-cell {
    background: #4affda00;
  }
}
</style>
